package com.zyx.eduservice.controller;


import com.zyx.commonutils.R;
import com.zyx.eduservice.client.VodClient;
import com.zyx.eduservice.entity.EduVideo;
import com.zyx.eduservice.service.EduVideoService;
import com.zyx.servicebase.exceptionhandler.GuliException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author zyx
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Resource
    private EduVideoService videoService;

    @Resource
    private VodClient vodClient;

    /**
     * 添加小节
     * @param eduVideo
     * @return
     */
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    /**
     * 删除小节
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {

        EduVideo video = videoService.getById(id);
        String sourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(sourceId)){
            R result = vodClient.removeAlyVideo(sourceId);
            if (result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，熔断器。。。");
            }
        }

        videoService.removeById(id);
        return R.ok();
    }

    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId){
        EduVideo id = videoService.getById(videoId);
        return R.ok().data("video",id);
    }

    /**
     * 修改小节
     */
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody  EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }

}

